﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BestBuy.DAL;

namespace BestBuy.PageAdmin
{
    public partial class PageStaffManager : System.Web.UI.Page
    {
        private CNhanVien _cnv = new CNhanVien();
        private static int _currentPage = 1;

        protected void Page_Load(object sender, EventArgs e)
        {
            pnError.Visible = false;
            pnEmtry.Visible = false;
            if (!IsPostBack)
            {
                setEnableCBB();
                Filter();
            }
        }

        protected void setCheckedFalseCHB()
        {
            chbSex.Checked = false;
            chbTypeUserName.Checked = false;
            chbName.Checked = false;
        }
        protected void setEnableCBB()
        {
            cbbSex.Enabled = false;
            cbbTypeUserName.Enabled = false;
            txtName.Enabled = false;

        }

        protected void XoaThongTinLoc()
        {
            CFilterInfoStaff.Name = "";
            CFilterInfoStaff.LoaiND = 0;
            CFilterInfoStaff.Sex = "";
            //CFilterInfoStaff.CurrentPage = 1;
        }

        public string CheckGioiTinh(object obj)
        {
            bool b = bool.Parse(obj.ToString());
            if (!b)
                return "Male";
            else
                return "Female";
        }

        private void Filter()
        {
            _currentPage = 1;
            string strQuery = UpdateFilterInfo();
            LoadTotalPageNumber(strQuery);
            LoadGV(strQuery);
        }

        protected void LoadTotalPageNumber(string query)
        {
            int soLuongTrang = _cnv.slTrangDuLieu(gridview.PageSize, query);
            if (soLuongTrang == 1)
            {
                _currentPage = 1;
            }
            lbTrang.Text = soLuongTrang.ToString();
        }

        private void LoadGV(string strQuery)
        {
            txtTrangHienTai.Text = _currentPage.ToString();
            CFilterInfoStaff.CurrentPage = _currentPage;
            int size = gridview.PageSize;
            int begin = (_currentPage - 1) * size;
            List<NhanVien> list = _cnv.GetList(strQuery, begin, size);
            if (list.Count > 0)
            {
                gridview.DataSource = list;
                gridview.DataBind();
                pnEmtry.Visible = false;
                lbGridViewEmtry.Text = "";
                pnPhanTrang.Visible = true;
            }
            else
            {
                pnEmtry.Visible = true;
                pnEmtry.CssClass = "InLeft info message";
                pnPhanTrang.Visible = false;
                gridview.DataSource = null;
                gridview.DataBind();
                lbGridViewEmtry.Text = "There are no data.";

            }
        }

        protected string GetFilterInfo()
        {
            string where = "";
            setCheckedFalseCHB();
            setEnableCBB();
            if (!CFilterInfoStaff.Name.Equals(""))
            {
                chbName.Checked = true;
                txtName.Enabled = true;
                txtName.Text = CFilterInfoUser.Name;
                where += " and dl.HoTen like N'%" + CFilterInfoStaff.Name + "%'";
            }
            if (CFilterInfoStaff.LoaiND > 0)
            {
                chbTypeUserName.Checked = true;
                cbbTypeUserName.Enabled = true;
                cbbTypeUserName.SelectedValue = CFilterInfoStaff.LoaiND.ToString();
                where += " and dl.LoaiND=" + CFilterInfoStaff.LoaiND.ToString();
            }
            if (!CFilterInfoStaff.Sex.Equals(""))
            {
                chbSex.Checked = true;
                cbbSex.Enabled = true;
                cbbSex.SelectedValue = CFilterInfoStaff.Sex;
                where += " and dl.GioiTinh= '" + CFilterInfoStaff.Sex + "'";
            }
            return where;
        }

        protected string UpdateFilterInfo()
        {
            XoaThongTinLoc();
            string where = "";
            int ma;
            if (chbTypeUserName.Checked)
            {
                ma = int.Parse(cbbTypeUserName.SelectedValue.ToString());
                where += " and dl.LoaiND=" + ma.ToString();
                CFilterInfoStaff.LoaiND = ma;
            }
            if (chbSex.Checked)
            {
                where += " and dl.GioiTinh= '" + cbbSex.SelectedValue + "'";
                CFilterInfoStaff.Sex = cbbSex.SelectedValue;
            }
            if (chbName.Checked && !txtName.Text.Equals(""))
            {
                where += " and dl.HoTen like N'%" + txtName.Text + "%'";
                CFilterInfoStaff.Name = txtName.Text;
            }
            return where;
        }

        protected void chbName_CheckedChanged(object sender, EventArgs e)
        {
            if (chbName.Checked)
            {
                txtName.Enabled = true;
            }
            else
            {
                txtName.Enabled = false;
            }
        }

        protected void chbTypeUserName_CheckedChanged(object sender, EventArgs e)
        {
            if (chbTypeUserName.Checked)
            {
                cbbTypeUserName.Enabled = true;
            }
            else
            {
                cbbTypeUserName.Enabled = false;
            }
        }

        protected void chbSex_CheckedChanged(object sender, EventArgs e)
        {
            if (chbSex.Checked)
            {
                cbbSex.Enabled = true;
            }
            else
            {
                cbbSex.Enabled = false;
            }
        }

        protected void lbtnXoa_Click(object sender, EventArgs e)
        {
            lbError.Text = "";
            int size = gridview.Rows.Count;
            bool kq = false;
            int idtaikhoan;
            string strquery = GetFilterInfo();
            for (int i = 0; i <= size - 1; i++)
            {
                try
                {
                    CheckBox pXoa = (CheckBox)gridview.Rows[i].FindControl("chkXoa");
                    if (pXoa.Checked)
                    {
                        idtaikhoan = Convert.ToInt32(((Label)gridview.Rows[i].FindControl("lbID")).Text);
                        kq = _cnv.LockAccount(idtaikhoan);
                        if (!kq)
                        {
                            pnError.Visible = true;
                            pnError.CssClass = "InLeft error message";
                            lbError.Text = lbError.Text + "Error. The data that this code : " + idtaikhoan.ToString() + " can not be locked.Please try again <br/>";
                        }
                    }
                }
                catch
                {
                    LoadTotalPageNumber(strquery);
                    LoadGV(strquery);
                }
            }
            if (kq)
            {
                pnError.Visible = true;
                pnError.CssClass = "InLeft success message";
                lbError.Text = "Lock account successfully";
            }
            LoadTotalPageNumber(strquery);
            LoadGV(strquery);
        }

        protected void lbtnBegin_Click(object sender, EventArgs e)
        {
            _currentPage = 1;
            string strQuery = GetFilterInfo();
            LoadGV(strQuery);
        }

        protected void lbtnPre_Click(object sender, EventArgs e)
        {
            if (_currentPage > 1)
            {
                _currentPage--;
                string strQuery = GetFilterInfo();
                LoadGV(strQuery);
            }
        }

        protected void lbtnNext_Click(object sender, EventArgs e)
        {
            if (_currentPage < int.Parse(lbTrang.Text))
            {
                _currentPage++;
                string strQuery = GetFilterInfo();
                LoadGV(strQuery);
            }
        }

        protected void lbtnEnd_Click(object sender, EventArgs e)
        {
            _currentPage = int.Parse(lbTrang.Text);
            string strQuery = GetFilterInfo();
            LoadGV(strQuery);
        }

        protected void lbtnToi_Click(object sender, EventArgs e)
        {
            int goToPage = Convert.ToInt32(txtTrangHienTai.Text);
            int soLuongTrang = int.Parse(lbTrang.Text);
            if (goToPage > soLuongTrang)
            {
                _currentPage = soLuongTrang;
            }
            else if (goToPage <= soLuongTrang)
            {
                _currentPage = goToPage;
            }
            string strQuery = GetFilterInfo();
            LoadGV(strQuery);
        }

        protected void btnFilter_Click(object sender, EventArgs e)
        {
            Filter();
        }
    }
}